using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Sandia.MVCFramework.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Sandia.MVCFramework.View
{
    /// <summary>
    /// A general progress bar form
    /// </summary>
    public partial class ProgressBarForm : Form
    {
        /// <summary>
        /// Default constructor
        /// </summary>
        public ProgressBarForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Closes form with invoke required checking
        /// </summary>
        public void CloseWithInvokeCheck()
        {
            if (this.InvokeRequired)
            {
                try
                {
                    // If so, call invoke on this method 
                    this.BeginInvoke(new MethodInvoker(CloseWithInvokeCheck));
                }
                catch (Exception e)
                {
                    // Use exception handling policy to determine what to do
                    bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                    if (rethrow)
                    {
                        throw;
                    }
                }
            }
            else
            {
                try
                {
                    this.Close();
                }
                catch (Exception e)
                {
                    // Use exception handling policy to determine what to do
                    bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                    if (rethrow)
                    {
                        throw;
                    }
                }
            }
        }

        /// <summary>
        /// Shows form with invoke required checking
        /// </summary>
        public void ShowWithInvokeCheck()
        {
            if (this.InvokeRequired)
            {
                try
                {
                    // If so, call invoke on this method 
                    this.BeginInvoke(new MethodInvoker(ShowWithInvokeCheck));
                }
                catch (Exception e)
                {
                    // Use exception handling policy to determine what to do
                    bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                    if (rethrow)
                    {
                        throw;
                    }
                }
            }
            else
            {
                try
                {
                    //WindowHandleWrapper windowHandleWrapper = new WindowHandleWrapper( 
                    //    System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
                    //this.Show(windowHandleWrapper);
                    this.Show();
                }
                catch (Exception e)
                {
                    // Use exception handling policy to determine what to do
                    bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                    if (rethrow)
                    {
                        throw;
                    }
                }
            }
        }

        /// <summary>
        /// Hides form with invoke required checking
        /// </summary>
        public void HideWithInvokeCheck()
        {
            if (this.InvokeRequired)
            {
                try
                {
                    // If so, call invoke on this method 
                    this.BeginInvoke(new MethodInvoker(HideWithInvokeCheck));
                }
                catch (Exception e)
                {
                    // Use exception handling policy to determine what to do
                    bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                    if (rethrow)
                    {
                        throw;
                    }
                }
            }
            else
            {
                try
                {
                    this.Hide();
                }
                catch (Exception e)
                {
                    // Use exception handling policy to determine what to do
                    bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                    if (rethrow)
                    {
                        throw;
                    }
                }
            }
        }

        /// <summary>
        /// Shows form as dialog with invoke required checking
        /// </summary>
        public void ShowDialogWithInvokeCheck()
        {
            if (this.InvokeRequired)
            {
                try
                {
                    // If so, call invoke on this method 
                    this.BeginInvoke(new MethodInvoker(ShowDialogWithInvokeCheck));
                }
                catch (Exception e)
                {
                    // Use exception handling policy to determine what to do
                    bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                    if (rethrow)
                    {
                        throw;
                    }
                }
            }
            else
            {
                try
                {
                    //WindowHandleWrapper windowHandleWrapper = new WindowHandleWrapper(
                    //    System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);                
                    //this.ShowDialog(windowHandleWrapper);
                    this.ShowDialog();
                }
                catch (Exception e)
                {
                    // Use exception handling policy to determine what to do
                    bool rethrow = ExceptionPolicy.HandleException(e, "Default Policy");
                    if (rethrow)
                    {
                        throw;
                    }
                }
            }
        }
    }
}